package property;

import java.util.Hashtable;

import database.RecordSearch;
import dbmanager.Column;
import dbmanager.DBManager;
import dbmanager.GetIdName;
import utility.Input;

public class Property 
{
	String mid="NO$";
	String pid="NO$";
	String Ref="NO$";
	int pv_index = -1;
	int vt_index = -1;
	int Ref_index = -1;
	GetIdName gin;
	String ref;
	DBManager sqlDB;
	
	public Property(DBManager db)
	{
		 this.sqlDB = db;
		gin = new GetIdName(db);
	}
	
	public void setDatabaseObject(DBManager sqlDB) 
	{
		this.sqlDB = sqlDB;		
		gin = new GetIdName(sqlDB);
	}
	
	public void setMid(String mid)
	{
		this.mid = mid;
	}
	public void setPid(String pid)
	{
		this.pid = pid;
	}
	public void setRef(String Ref)
	{
		this.Ref = Ref;
	}
	public String getValue()
	{
		try {
			if(mid.equals("NO$"))
			{
				return "error";
			}
			String path = Input.MASTERREPORTSPATH+mid+"//"+mid+"_mid"+Input.FILRFORMAT;
			try 
			{
				pv_index = Column.pv_index-1;
				vt_index = Column.vt_index-1;
				Ref_index = Column.ref_index-1;
			}
			catch (Exception e) {
			}
			
			//GET PROPERTY
			Hashtable<Object, Object> table = new Hashtable<Object, Object>();
			if(pid.equals("NO$")==false)
				table.put("pid", pid);
			if(Ref.equals("NO$")==false)
				table.put("ref", Ref);
			table.put("mid", mid);
			/*SearchRecord sr = new SearchRecord(path);
			sr.fileOpen();
			String[] record = sr.getRecordSet(table);
			sr.fileClose();*/
			table.put("td","null");
			RecordSearch ps = new RecordSearch(sqlDB);
			ps.setConditionMap(table);
			String record [] = ps.getArrayFromResultSet();
			try 
			{
				record = record[0].split("\t");
				ref = record[Ref_index];
			} 
			catch (Exception e) 
			{
			}
			String pv = record[pv_index]; 
			
			if(pv.equals("0"))
				pv = record[vt_index];
			else
				pv = gin.getItem(pv);
			return pv;
		}
		catch (Exception e) 
		{
			e.printStackTrace();
			return "no$";
		}
		
	}
	public String getRef()
	{
		return ref;
	}
}
